package sample;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import sample.entity.ConnectionEntity;
import sample.layout.Body;
import sample.layout.HMenuBar;
import sample.layout.HTreeView;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        double w = 1000;
        double h = 775;
        ConnectionEntity.connectionEntity.setDbname("lefan");
        ConnectionEntity.connectionEntity.setPassword("123456");
        ConnectionEntity.connectionEntity.setUsername("admin");
        BorderPane root = new BorderPane();//原文出自【易百教程】，商业转载请联系作者获得授权，非商业请保留原文链接：https://www.yiibai.com/javafx/javafx_menu.html
        HMenuBar menuBar = new HMenuBar();
        SplitPane splitPane = new SplitPane();
        Body body = new Body();

        HTreeView hTreeView = new HTreeView(body);
        double prefWidth = hTreeView.getTree().getPrefWidth();
        body.setPrefSize(w - prefWidth, h);
        splitPane.getItems().add(0, hTreeView.getTree());
        splitPane.getItems().add(1, body);

        primaryStage.setTitle("数据库管理工具");
        Scene value = new Scene(root, w, h);

        value.getStylesheets().add(getClass().getResource("app.css").toExternalForm());
        primaryStage.setScene(value);
        primaryStage.show();


        root.setTop(menuBar);
        root.setCenter(splitPane);
//primaryStage.setResizable(false);

        primaryStage.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                System.out.println(newValue);

                double prefWidth = hTreeView.getTree().getPrefWidth();
                double v = newValue.doubleValue();
                body.setPrefSize(v - prefWidth, h);
            }
        });
    }
}
